//
//  RegisterViewController.m
//  MJApp
//
//  Created by 王亮 on 15/8/25.
//  Copyright (c) 2015年 王亮. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterOneCollectionViewCell.h"
#import "RegisterTwoCollectionViewCell.h"
#import "RegisterThreeCollectionViewCell.h"
#import "RegisterFinishViewController.h"

@interface RegisterViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,CellProtocol>

@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UILabel *descLabel;
@property(nonatomic,strong)NSString *nameStr;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initUI {
    
    self.view.backgroundColor = Color_Back_Black;
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    closeBtn.center = CGPointMake(IOS_IPHONE_WINDOW_WIDE - CGRectGetWidth(closeBtn.frame) * 0.5 - 10, 64 - CGRectGetHeight(closeBtn.frame) * 0.5);
    [closeBtn setImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [self.view addSubview:closeBtn];
    [closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, CGRectGetMinY(closeBtn.frame), 30, 30)];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [self.view addSubview:backBtn];
    [backBtn addTarget:self action:@selector(up) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(closeBtn.frame), 90, 90)];
    headImageView.center = CGPointMake(CGRectGetWidth(self.view.frame) / 2, headImageView.center.y);
    headImageView.image = [UIImage imageNamed:@"head"];
    headImageView.layer.cornerRadius = CGRectGetWidth(headImageView.frame) / 2;
    headImageView.clipsToBounds = YES;
    [self.view addSubview:headImageView];
    
    CGFloat wide = 0.6 * CGRectGetWidth(self.view.frame);
    CGFloat centerX = CGRectGetWidth(self.view.frame) / 2;
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(headImageView.frame) + 20,wide, 30)];
    nameLabel.text = @"会员注册";
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.font = [UIFont boldSystemFontOfSize:20];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.center = CGPointMake(centerX, nameLabel.center.y);
    [self.view addSubview:nameLabel];
    
    UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(nameLabel.frame) + 20, centerX * 2 - 20, 30)];
    descLabel.text = @"输入手机号>输入验证码>设置密码";
    descLabel.textAlignment = NSTextAlignmentCenter;
    descLabel.textColor = RGB(87, 83, 116);
    NSMutableArray *array = [NSMutableArray array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"text"] = @"输入手机号>";
    dic[@"font"] = descLabel.font;
    dic[@"color"] = [UIColor whiteColor];
    [array addObject:dic];
    [descLabel addMoreStates:array];
    [self.view addSubview:descLabel];
    self.descLabel = descLabel;
    
    CGFloat conHigh = CGRectGetHeight(self.view.frame) - (CGRectGetMaxY(descLabel.frame) + 10);
    CGRect frame = CGRectMake(0, CGRectGetMaxY(descLabel.frame) + 10, centerX * 2, conHigh);
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(IOS_IPHONE_WINDOW_WIDE, conHigh);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.scrollEnabled = NO;
    collectionView.pagingEnabled = YES;
    collectionView.backgroundColor = Color_Back_Black;
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];
    [collectionView registerClass:[RegisterOneCollectionViewCell class] forCellWithReuseIdentifier:@"RegisterOneCollectionViewCell"];
    [collectionView registerClass:[RegisterTwoCollectionViewCell class] forCellWithReuseIdentifier:@"RegisterTwoCollectionViewCell"];
    [collectionView registerClass:[RegisterThreeCollectionViewCell class] forCellWithReuseIdentifier:@"RegisterThreeCollectionViewCell"];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
}

- (void)closeBtn {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)up {
    
    CGFloat moveX = self.collectionView.contentOffset.x;
    CGFloat wide = CGRectGetWidth(self.collectionView.frame);
    int index = moveX / wide;
    if (moveX == 0) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        
        if (index == 1) {
            
            [self changeAllStr:@"输入手机号>输入验证码>设置密码" withStr:@"输入手机号>" lastStr:@"输入验证码>"];
        }else if (index == 2) {
            
            [self changeAllStr:@"输入手机号>输入验证码>设置密码" withStr:@"输入验证码>" lastStr:@"设置密码>"];
        }
        
        [self.collectionView setContentOffset:CGPointMake(wide * (index - 1), 0) animated:YES];
    }
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *cellID = @"CollectionViewCell";
    if (indexPath.row == 0) {
        
        cellID = @"RegisterOneCollectionViewCell";
    }else if (indexPath.row == 1) {
        
        cellID = @"RegisterTwoCollectionViewCell";
    }else if (indexPath.row == 2) {
        
        cellID = @"RegisterThreeCollectionViewCell";
    }
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.backgroundColor = Color_Back_Black;
    
    if (indexPath.row == 0) {
        
        RegisterOneCollectionViewCell *theCell = (RegisterOneCollectionViewCell *)cell;
        theCell.delegate = self;
    }else if(indexPath.row == 1) {
       
        RegisterTwoCollectionViewCell *theCell = (RegisterTwoCollectionViewCell *)cell;
        theCell.delegate = self;
    }else if(indexPath.row == 2) {
        
        RegisterThreeCollectionViewCell *theCell = (RegisterThreeCollectionViewCell *)cell;
        theCell.delegate = self;
    }
    
    return cell;
}

- (void)cellProtocolWithObj:(id)obj withDic:(id)dic {
    
    [self.view endEditing:YES];
    NSDictionary *infoDic = dic;
    NSInteger index = [infoDic[@"index"] integerValue];
    if (index == 0) {
        
        [self changeAllStr:@"输入手机号>输入验证码>设置密码" withStr:@"输入验证码>" lastStr:@"输入手机号>"];
        self.nameStr = infoDic[@"phone"];
        [self getCode:infoDic];
        
    }else if (index == 1) {
        
        BOOL isS = [self checkCode:infoDic];
        if (!isS) {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"输入验证码不正确"];
            return;
        }
        [self changeAllStr:@"输入验证码>设置密码>完成注册" withStr:@"设置密码>" lastStr:@"输入验证码>"];
    }else if (index == 2) {
        
        [self registerUser:infoDic];
        
    }
    CGFloat wide = CGRectGetWidth(self.collectionView.frame);
    [self.collectionView setContentOffset:CGPointMake(wide * (index + 1), 0) animated:YES];
    
}


- (void)changeAllStr:(NSString *)allStr withStr:(NSString *)str lastStr:(NSString *)lastStr {
    
    self.descLabel.text = allStr;
    self.descLabel.textColor = RGB(87, 83, 116);
    NSMutableArray *array = [NSMutableArray array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"text"] = str;
    dic[@"font"] = self.descLabel.font;
    dic[@"color"] = [UIColor whiteColor];
    [array addObject:dic];
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
    dic1[@"text"] = lastStr;
    dic1[@"font"] = self.descLabel.font;
    dic1[@"color"] = RGB(87, 83, 116);
    [array addObject:dic1];
    [self.descLabel addMoreStates:array];
}


- (void)getCode:(NSDictionary *)infoDic {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"user_name"] = infoDic[@"phone"];
    dic[@"type"] = @"1";
    [[AppData shareData] codeWithInfo:dic withFinishBlock:^(NSDictionary *dic) {
        
        if ([dic[@"status"] intValue] == 1) {
            
            [[NSUserDefaults standardUserDefaults] setObject:dic[@"data"][@"code"] forKey:@"code"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            self.descLabel.text = [NSString stringWithFormat:@"%@%@",self.descLabel.text,dic[@"data"][@"code"]];
        }
    }];
}

- (BOOL)checkCode:(NSDictionary *)dic {
    
    NSString *codeStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"code"];
    return [dic[@"code"] longLongValue] == [codeStr longLongValue];
}


- (void)registerUser:(NSDictionary *)infodic {
    
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"user_name"] = self.nameStr;
    dic[@"password"] = infodic[@"pass"];
    dic[@"nickname"] = infodic[@"name"];
    dic[@"type"] = @"0";
    [[AppData shareData] registerWithInfo:dic withFinishBlock:^(NSDictionary *dic) {
       
        if ([dic[@"status"] intValue] == 1) {
            
            RegisterFinishViewController *finishVC = [[RegisterFinishViewController alloc] init];
            [self.navigationController pushViewController:finishVC animated:YES];
        }
    }];
}

@end
